package com.ruoyi.points.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.youyou.domain.SysYouYouUser;
import com.ruoyi.youyou.service.ISysYouYouUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.points.domain.PointsRecord;
import com.ruoyi.points.service.IPointsRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.points.service.ISysUserProfileService; // ssh修改
import com.ruoyi.points.domain.SysUserProfile; // ssh修改

/**
 * 积分记录Controller
 *
 * @author jion
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/points/record")
public class PointsRecordController extends BaseController {
    @Autowired
    private IPointsRecordService pointsRecordService;
    @Autowired
    private ISysYouYouUserService sysYouYouUserService;
    @Autowired
    private ISysUserProfileService sysUserProfileService;
    // 一对一

@GetMapping("/allWithUser")
public TableDataInfo getPointsRecordAndUser( PointsRecord pointsRecord) {
    System.out.println(pointsRecord);
    startPage();
    List<PointsRecord> records = pointsRecordService.selectPointsRecordAndUser(pointsRecord);
//   sysYouYouUserService.selectSysYouYouUserList();
    return getDataTable(records);
}
  /**
     * 查询积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('points:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(PointsRecord pointsRecord) {
        startPage();
        List<PointsRecord> list = pointsRecordService.selectPointsRecordList(pointsRecord);
        return getDataTable(list);
    }

    /**
     * 导出积分记录列表
     */
    @PreAuthorize("@ss.hasPermi('points:record:export')")
    @Log(title = "积分记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PointsRecord pointsRecord) {
        List<PointsRecord> list = pointsRecordService.selectPointsRecordList(pointsRecord);
        ExcelUtil<PointsRecord> util = new ExcelUtil<PointsRecord>(PointsRecord.class);
        util.exportExcel(response, list, "积分记录数据");

    }

    /**
     * 获取积分记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('points:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pointsRecordService.selectPointsRecordById(id));
    }

    /**
     * 新增积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:add')")
    @Log(title = "积分记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PointsRecord pointsRecord) {
        return toAjax(pointsRecordService.insertPointsRecord(pointsRecord));
    }

    /**
     * 修改积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:edit')")
    @Log(title = "积分记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PointsRecord pointsRecord) {
        return toAjax(pointsRecordService.updatePointsRecord(pointsRecord));
    }

    /**
     * 删除积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:remove')")
    @Log(title = "积分记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pointsRecordService.deletePointsRecordByIds(ids));
    }

    /**
     * ssh修改
     * 查询用户总积分
     */
    @PreAuthorize("@ss.hasPermi('points:record:query')")
    @GetMapping("/total/{userId}")
    public AjaxResult getTotalPoints(@PathVariable Long userId) {
        SysUserProfile profile = sysUserProfileService.selectByUserId(userId); // ssh修改
        return AjaxResult.success(profile != null ? profile.getPoints() : 0);
    }

    /**
     * ssh修改
     * 用户积分变动（如消费/获赠/扣减）
     *
     * @param pointsRecord 积分记录
     */
    @PreAuthorize("@ss.hasPermi('points:record:edit')")
    @Log(title = "积分记录", businessType = BusinessType.UPDATE)
    @PostMapping("/change")
    public AjaxResult changePoints(@RequestBody PointsRecord pointsRecord) {
        // 直接用 pointsRecord.getUserId() 等获取参数
        return toAjax(pointsRecordService.changeUserPoints(
                pointsRecord));
    }
}
